package com.example.griddemo;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.example.griddemo.fragment.CategoryFragment;
import com.example.griddemo.fragment.CategoryFragment;
import com.example.griddemo.fragment.HomeFragment;
import com.example.griddemo.fragment.MineFragment;
import com.example.griddemo.fragment.OrderFragment;
import com.example.griddemo.viewmodel.Category;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {


    private BottomNavigationView bottomNavigationView;
    private HomeFragment homeFragment;
    private CategoryFragment carFragment;
    private OrderFragment orderFragment;
    private MineFragment mineFragment;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        selecteFragment(0);
        bottomNavigationView = findViewById(R.id.bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
              if(item.getItemId()==R.id.home)
              {
                  selecteFragment(0);
              }
              else if(item.getItemId()==R.id.car){
                  selecteFragment(1);
                }
              else if(item.getItemId()==R.id.order){
                  selecteFragment(2);
              }
              else {
                  selecteFragment(3);
              }
             return  true;
            }
        });
    }

    private void selecteFragment(int index) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hideFragment(fragmentTransaction);

        if (index == 0) {
            if (homeFragment == null) {
                homeFragment = new HomeFragment();
                fragmentTransaction.add(R.id.content, homeFragment);
            } else {
                fragmentTransaction.show(homeFragment);
            }
        } else if (index == 1) {
            if (carFragment == null) {
                carFragment = new CategoryFragment();
                fragmentTransaction.add(R.id.content, carFragment);
            }
            else {
                fragmentTransaction.show(carFragment);
            }
        } else if (index == 2) {
            if (orderFragment == null) {
                orderFragment = new OrderFragment();
                fragmentTransaction.add(R.id.content, orderFragment);
            }
            else {
                fragmentTransaction.show(orderFragment);
            }
        } else if (index == 3) {
            if (mineFragment == null) {
                mineFragment = new MineFragment();
                fragmentTransaction.add(R.id.content, mineFragment);
            }
            else {
                fragmentTransaction.show(mineFragment);
            }
        }


             fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (homeFragment != null) {
            fragmentTransaction.hide(homeFragment);
        }
        if (carFragment != null) {
            fragmentTransaction.hide(carFragment);
        }
        if (orderFragment != null) {
            fragmentTransaction.hide(orderFragment);
        }
        if (mineFragment != null) {
            fragmentTransaction.hide(mineFragment);
        }
    }


}